package com.zl.study.mybatis.demo;

import com.zl.study.mybatis.demo.entity.Blog;
import com.zl.study.mybatis.demo.mapper.BlogMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Driver;
import java.sql.DriverManager;
import java.util.Enumeration;

public class Main {
  public static void main(String[] args) throws IOException {

    ClassLoader cl = ClassLoader.getSystemClassLoader();
    System.out.println(cl);


    SqlSessionFactory ssf = MyBatisUtils.getSqlSessionFactory();

    // 默认autoCommit是false，即不自动提交
    try(SqlSession session = ssf.openSession()) {
      BlogMapper blogMapper = session.getMapper(BlogMapper.class);
      Blog blog = blogMapper.selectBlog(1);
      if(null != blog) {
        // 返回值为1
        System.out.println(blog.getId());

        // 由于local cache（即一级缓存）的存在
        // 重新查找时会直接使用已有的blog对象，而blog对象的id已经设置为1，
        // 因此下面代码的输出是0
        blog.setId(0);
        blogMapper.selectBlog(1);
        System.out.println(blog.getId());
      }

      BlogMapper blogMapper2 = session.getMapper(BlogMapper.class);
      System.out.println(blogMapper2);

      blog = blogMapper.selectBlog(1);

      if(null != blog) {
        // 这里还是输出了0，虽然mapper的代理不一样了，但是一级缓存仍然起作用
        System.out.println(blog.getId());
      }

      /*Blog blog2 = new Blog();
      blog2.setId(4);
      blogMapper.insert(blog2);
      session.commit();*/
    }

    /*Enumeration<Driver> drivers = DriverManager.getDrivers();
    while(drivers.hasMoreElements()) {
      System.out.println(drivers.nextElement());
    }*/

  }
}
